$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'orth/teamManage/list',
        datatype: "json",
        colModel: [
            { label: '头像', name: 'picUrl',sortable:false, width: 80, formatter: function(value, options, row){

                return   value==undefined || value==null || value=="" ? '<image src="/statics/img/noImage.png" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>' : '<image src="'+value+'" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>';
            }},
            { label: '用户ID', name: 'id_', index: "id_", width: 35, key: true,hidden:true},
            { label: '姓名', name: 'name', width: 70 },
            { label: '医学职称', name: 'medicalTitle', width: 100 },
            { label: '医院', name: 'hospital', width: 100 },
            { label: '所属团队', name: 'type', width: 80, formatter: function(value, options, row){
                switch(value)
                {
                    case 1:return "关节修复与重建";
                    case 2:return "关节翻修";
                    case 3:return "下肢畸形矫正";
                    case 4:return "骨与关节感染";
                    case 5:return "风湿关节病";
                    default: return "";
                }
            }},
            { label: '更新时间', name: 'updateTime', width: 80,sortable:false}
        ],
        viewrecords: true,

        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $('.form-horizontal').validationEngine('attach',{
        autoHidePrompt: true,
        autoHideDelay: 2000,
        maxErrorsPerField: 1
    });
});
/*
function uploadImage(id,name)
{
    $(id).fileupload({
        url: baseURL + "fileUpload/image",
        type: "POST",
        autoUpload: true,
        sequentialUploads: true
    }).bind('fileuploadprogress', function (e, data) {
        if(name=="picUrl")
            vm.picUrl = data.originalFiles[0].name;
    }).bind('fileuploaddone', function (e, data) {

        if(data.result.errcode==0){
            if(name=="picUrl")
                vm.team.picUrl = data.result.data;
            alert("上传成功！");
        }else{
            alert("上传失败！");
        }
    });
}*/


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            name: null
        },
        requestUrl:baseURL+"fileUpload/image",
        picUrl:"",
        showList: true,
        title:null,
        team:{
            status:0,
            type:'1'
        },
        token:{token:localStorage.getItem("token")},
        experts:[],/*保存所有可选的专家 姓名和id,例如 text:刘健，value:12 */
        items:[{text:'关节修复与重建',value:'1'},{text:'关节翻修',value:'2'},{text:'下肢畸形矫正',value:'3'},{text:'骨与关节感染',value:'4'},{text:'风湿关节病',value:'5'}],
    },
    methods: {
        handleRemove0(file, fileList) {
            vm.picUrl="";
        },
        handlePreview0(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccess0(res, file) {
            vm.picUrl=res.data;
        },
        query: function () {
            jQuery("#jqGrid").jqGrid('setGridParam',{page:1});
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.team = {status:0,type:'1',picUrl:""};
            vm.picUrl="";
            vm.getExperts(1,vm.team.type);
        },
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getUser(userId);

        },
        del: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "orth/teamManage/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {

            if(!$(".form-horizontal").validationEngine("validate")) {
                $(".formError").show();
                return;
            }
            /*if(vm.validator()){
                return ;
            }*/
            vm.team.picUrl=vm.picUrl;
            if(vm.team.picUrl=="")
            {
                alert("请上传头像");
                return;
            }
            var url = vm.team.id_ == null ? "orth/teamManage/save" : "orth/teamManage/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.team),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(id){
            $.get(baseURL + "orth/teamManage/info/"+id, function(r){
                vm.team = r.team;
                vm.picUrl=vm.team.picUrl;
            });
        },
        getExperts:function(ele,type){

            var temp=type === undefined ? ele.target.value : type;
            console.warn(type === undefined);
            $.ajax({
                type: "POST",
                url: baseURL + "orth/teamManage/getMembers?type="+temp,
                contentType: "application/json",
                success: function(r){
                    if(r.code == 0){
                        vm.experts=r.members;

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            $(".formError").hide();
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {

        }
    }
});